Call Button Click Event on Press Enter Key In ASP.NET

This article will discuss how to implement a call button click event on pressing of the Enter Key. In many websites you will see that the user can press the Enter key to call the Button Click event. Here, in this application we use a TextBox to search items and we need to call a search function once the user enters the keywords in the text box and presses the "Enter" button. Here we can use JavaScript to call a server-side function in ASP.NET.

Now drag and drop one TextBox, one Button and one Label control on the form.

<asp:TextBox ID="TextBox1" runat="server" Width="230px" MaxLength="50"

Height="20px"></asp:TextBox>
<
asp:Button ID="Button1" runat="server" Text="Submit1" OnClick="ButtonIN_Click">

</
asp:Button>         

<br />

<br />
<
asp:Label ID="message" runat="server"></asp:Label>
 

JavaScript Function

Now create a JavaScript function in the head section to call the Button Click event when the Enter key is pressed, as in the following:

<Script>

  function funfordefautenterkey1(btn, event) {
      if (document.all) {

          if (event.keyCode == 13) {

              event.returnValue = false;

              event.cancel = true;

              btn.click();

          }

     }                

</Script>

The following is the source code for the design of our ".ASPX" page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Refreshpage.aspx.cs" Inherits="Refreshpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title></title
>

    <Script
>

        function funfordefautenterkey1(btn, event) {

            if (document.all) {

                if (event.keyCode == 13) {

                    event.returnValue = false;

                    event.cancel = true;

                    btn.click();

                }

            }          

        </Script>

</head>
<
body>
    <form id="form1" runat
="server">

    <div
>

        <asp:TextBox ID="TextBox1" runat="server" Width="230px" MaxLength
="50"

            Height="20px"></asp:TextBox
>

        <asp:Button ID="Button1" runat="server" Text="Submit1" OnClick
="ButtonIN_Click">

        </asp:Button>     

        <br
/>

        <br
/>

        <asp:Label ID="message" runat="server"></asp:Label
>

    </div
>

    </form
>

</
body>
</
html>

Now call the JavaScript Function on the Server:

txt.Attributes.Add("onkeydown", "funfordefautenterkey1(" + defaultButton.ClientID + ",event)");

.cs code

using System;

using System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls; 

public partial class Refreshpage : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)

    {

        SetDefaultButton(TextBox1,  Button1);                      

    } 

    private void SetDefaultButton(TextBox txt, Button defaultButton)

    {       
        txt.Attributes.Add("onkeydown", "funfordefautenterkey1(" + defaultButton.ClientID + ",event)");

    }

    protected void ButtonIN_Click(object sender, EventArgs e)

    {
       message.Text = TextBox1.Text;

     }

}

Now to see the output press F5 to execute.

 

Output

 

Call-Button-Click-Event-on-Press-Enter-Key1.jpg

Now enter the text in the "TextBox" and click on the Enter key.

Call-Button-Click-Event-on-Press-Enter-Key2.jpg

If you click on the Button Control then you will get the same output.

Up Next
    Ebook Download
    View all
    Learn
    View all